package com.cw.jeeyt.common.utils.process;

import java.util.HashMap;
import java.util.Map;

import com.cw.jeeyt.common.StringUtils;
import com.cw.jeeyt.common.config.Encodes;
import com.cw.jeeyt.common.process.Flow;
import com.cw.lang.common.config.Global;
import com.cw.lang.common.utils.TimeUtils;

/**
 * 流程工具
 *
 * @author liuruijun
 * @version 2018-08-03
 */
public class FlowableUtils {

    /**
     * 定义流程定义KEY，必须以“PD_”开头
     * 组成结构：string[]{"流程标识","业务主表表名"}
     */
    private final static Map<String, String> PRO_DEF_MAP = new HashMap<String, String>() {
        {
            put("leave", "process_leave");
        }
    };

    public static String getBusinessTable(String key) {
        return PRO_DEF_MAP.get(key);
    }

    /**
     * 获取流程表单URL
     *
     * @param formKey
     * @param act 表单传递参数
     * @return
     */
    public static String getFormUrl(String formKey, Flow act) {

        StringBuilder formUrl = new StringBuilder();

        String formServerUrl = Global.getConfig("activiti.form.server.url");
        if (StringUtils.isBlank(formServerUrl)) {
            formUrl.append(Global.getAdminPath());
        } else {
            formUrl.append(formServerUrl);
        }

        formUrl.append(formKey)
            .append(formUrl.indexOf("?") == -1 ? "?" : "&");
        formUrl.append("act.taskId=")
            .append(act.getTaskId() != null ? act.getTaskId() : "");
        formUrl.append("&act.taskName=")
            .append(act.getTaskName() != null ? Encodes.urlEncode(act.getTaskName()) : "");
        formUrl.append("&act.taskDefKey=")
            .append(act.getTaskDefKey() != null ? act.getTaskDefKey() : "");
        formUrl.append("&act.procInsId=")
            .append(act.getProcInsId() != null ? act.getProcInsId() : "");
        formUrl.append("&act.procDefId=")
            .append(act.getProcDefId() != null ? act.getProcDefId() : "");
        formUrl.append("&act.status=")
            .append(act.getStatus() != null ? act.getStatus() : "");
        formUrl.append("&id=")
            .append(act.getBusinessId() != null ? act.getBusinessId() : "");

        return formUrl.toString();
    }

    public static String toTimeString(long time) {
        TimeUtils t = new TimeUtils(time);
        int day = t.get(TimeUtils.DAY);
        int hour = t.get(TimeUtils.HOUR);
        int minute = t.get(TimeUtils.MINUTE);
        int second = t.get(TimeUtils.SECOND);
        StringBuilder sb = new StringBuilder();
        if (day > 0) {
            sb.append(day)
                .append("天");
        }
        if (hour > 0) {
            sb.append(hour)
                .append("时");
        }
        if (minute > 0) {
            sb.append(minute)
                .append("分");
        }
        if (second > 0) {
            sb.append(second)
                .append("秒");
        }
        return sb.toString();
    }
}
